#include "../Common/Net.hpp"
#include "../Common/Message.hpp"
#include <thread>
#include "../Common/Dispatcher.hpp"
#include"../Client/Rpc_callor.hpp"
// using ConnectionCallback = std::function<void(const BaseConnection::ptr &conn)>;
// using MessageCallback = std::function<void(const BaseConnection::ptr &conn, const BaseMessage::ptr &buf)>;
// using CloseCallback = std::function<void(const BaseConnection::ptr &conn)>;

void OnRpcMessage(const zgwrpc::BaseConnection::ptr &conn, const zgwrpc::BaseMessage::ptr &buf)
{
    cout << "OnRpcMessage:" << buf->serialize() << endl;
}


int main()
{
    zgwrpc::client::Requestor::ptr requstor=make_shared<zgwrpc::client::Requestor>();
    zgwrpc::client::RpcCaller::ptr rpccaller=make_shared<zgwrpc::client::RpcCaller>(requstor);
    
    auto rpc_callback=std::bind(&zgwrpc::client::Requestor::OnMessage,requstor.get(), placeholders::_1,placeholders::_2);
    zgwrpc::BaseClient::ptr client = zgwrpc::ClientFactory::create(8888, "127.0.0.1");


    zgwrpc::Dispatcher::ptr dispatcher = zgwrpc::DispatcherFactory::create();

    dispatcher->registerHandler<zgwrpc::RpcResponse>(zgwrpc::MType::RSP_RPC, rpc_callback);
    client->setMessageCallback(std::bind(&zgwrpc::Dispatcher::dispatch, dispatcher, std::placeholders::_1, std::placeholders::_2));


    Json::Value params;
    std::future<Json::Value> result;
    params["num1"]=90;
    params["num2"]=1;
    rpccaller->call(client->connection(),"Add",params,result);
    cout<<"result:"<<result.get()<<endl;


    while (true)
    {
        std::string str;
        cin >> str;
        Json::Value paramsgame;
        std::future<Json::Value> gameresult;
        paramsgame["game"]=str;
        rpccaller->call(client->connection(),"Game",paramsgame,gameresult);
        cout<<"result:"<<gameresult.get()<<endl;
    }

    std::this_thread::sleep_for(std::chrono::seconds(2));
    return 0;
}
